﻿using System.Linq;
using System.Web.UI.WebControls;
using ExtendedMvc.Tests.Model;
using ExtendedMvc.Tests.Model.Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ExtendedMvc.Tests {

	/// <summary>
	///This is a test class for ImageTest and is intended
	///to contain all ImageTest Unit Tests
	///</summary>
	[TestClass()]
	public class ImageTest {

		private static DummyDataSource database;
		private static ModelViewPage<Dummy> view;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext { get; set; }

		#region Additional test attributes
		// 
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		[ClassInitialize()]
		public static void MyClassInitialize(TestContext testContext) {
			database = new DummyDataSource();
			var dummy = database.Dummies.All().First();
			view = MvcMockHelpers.FakeViewPage("Index", dummy);
		}

		//Use ClassCleanup to run code after all tests in a class have run
		//[ClassCleanup()]
		//public static void MyClassCleanup() {
		//}
		//
		//Use TestInitialize to run code before running each test
		[TestInitialize()]
		public void MyTestInitialize() {
		}

		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//public void MyTestCleanup() {
		//}
		//
		#endregion

		/// <summary>
		///A test for Image Constructor
		///</summary>
		[TestMethod()]
		public void Image_ContentOnly() {
			// Arrange
			var informedSource = "~/images/dummy.jpg";
			var outputSource = "/images/dummy.jpg";
			var expectedOutput = string.Format(@"<image src=""{0}"" />", outputSource);

			// Act
			var image = view.Html.Image(informedSource);
			var output = image.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for Image Constructor
		///</summary>
		[TestMethod()]
		public void Image_WithId() {
			// Arrange
			var informedSource = "/images/dummy.jpg";
			var outputSource = "/images/dummy.jpg";
			var imageId = "imageId";
			var expectedOutput = string.Format(@"<image id=""{1}"" src=""{0}"" />", outputSource, imageId);

			// Act
			var image = view.Html.Image(informedSource, imageId);
			var output = image.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for Image Constructor
		///</summary>
		[TestMethod()]
		public void Image_ForModel() {
			// Arrange
			var dummy = database.Dummies.All().First();
			var expectedOutput = string.Format(@"<image src=""{0}"" />", dummy.BasicProperty);

			// Act
			var image = view.Html.Image(m => m.BasicProperty);
			var output = image.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for Image Constructor
		///</summary>
		[TestMethod()]
		public void Image_WithIntWidthOnly() {
			// Arrange
			var informedSource = "/images/dummy.jpg";
			var informedWidth = 200;
			var expectedOutput = string.Format(@"<image src=""{0}"" width=""{1}px"" />", informedSource, informedWidth);

			// Act
			var image = view.Html.Image(informedSource).Width(informedWidth);
			var output = image.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for Image Constructor
		///</summary>
		[TestMethod()]
		public void Image_WithStringWidthOnly() {
			// Arrange
			var informedSource = "/images/dummy.jpg";
			var informedWidth = "200%";
			var expectedOutput = string.Format(@"<image src=""{0}"" width=""{1}"" />", informedSource, informedWidth);

			// Act
			var image = view.Html.Image(informedSource).Width(informedWidth);
			var output = image.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for Image Constructor
		///</summary>
		[TestMethod()]
		public void Image_WithUnitWidthOnly() {
			// Arrange
			var informedSource = "/images/dummy.jpg";
			var informedWidth = new Unit(200);
			var expectedOutput = string.Format(@"<image src=""{0}"" width=""{1}"" />", informedSource, informedWidth.ToString());

			// Act
			var image = view.Html.Image(informedSource).Width(informedWidth);
			var output = image.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for Image Constructor
		///</summary>
		[TestMethod()]
		public void Image_WithIntHeightOnly() {
			// Arrange
			var informedSource = "/images/dummy.jpg";
			var informedHeight = 200;
			var expectedOutput = string.Format(@"<image height=""{1}px"" src=""{0}"" />", informedSource, informedHeight);

			// Act
			var image = view.Html.Image(informedSource).Height(informedHeight);
			var output = image.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for Image Constructor
		///</summary>
		[TestMethod()]
		public void Image_WithStringHeightOnly() {
			// Arrange
			var informedSource = "/images/dummy.jpg";
			var informedHeight = "200%";
			var expectedOutput = string.Format(@"<image height=""{1}"" src=""{0}"" />", informedSource, informedHeight);

			// Act
			var image = view.Html.Image(informedSource).Height(informedHeight);
			var output = image.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for Image Constructor
		///</summary>
		[TestMethod()]
		public void Image_WithUnitHeightOnly() {
			// Arrange
			var informedSource = "/images/dummy.jpg";
			var informedHeight = new Unit(200);
			var expectedOutput = string.Format(@"<image height=""{1}"" src=""{0}"" />", informedSource, informedHeight.ToString());

			// Act
			var image = view.Html.Image(informedSource).Height(informedHeight);
			var output = image.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for Image Constructor
		///</summary>
		[TestMethod()]
		public void Image_WithIntSize() {
			// Arrange
			var informedSource = "/images/dummy.jpg";
			var informedWidth = 300;
			var informedHeight = 200;
			var expectedOutput = string.Format(@"<image height=""{2}px"" src=""{0}"" width=""{1}px"" />", informedSource, informedWidth, informedHeight);

			// Act
			var image = view.Html.Image(informedSource).Size(informedWidth, informedHeight);
			var output = image.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for Image Constructor
		///</summary>
		[TestMethod()]
		public void Image_WithStringSize() {
			// Arrange
			var informedSource = "/images/dummy.jpg";
			var informedWidth = "100%";
			var informedHeight = "50%";
			var expectedOutput = string.Format(@"<image height=""{2}"" src=""{0}"" width=""{1}"" />", informedSource, informedWidth, informedHeight);

			// Act
			var image = view.Html.Image(informedSource).Size(informedWidth, informedHeight);
			var output = image.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for Image Constructor
		///</summary>
		[TestMethod()]
		public void Image_WithUnitSize() {
			// Arrange
			var informedSource = "/images/dummy.jpg";
			var informedWidth = new Unit(20, UnitType.Em);
			var informedHeight = new Unit(10, UnitType.Em); ;
			var expectedOutput = string.Format(@"<image height=""{2}"" src=""{0}"" width=""{1}"" />", informedSource, informedWidth.ToString(), informedHeight.ToString());

			// Act
			var image = view.Html.Image(informedSource).Size(informedWidth, informedHeight);
			var output = image.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}
	}
}
